package cn.jianyongjie.org.controller;

import cn.jianyongjie.basic.aoon.PreAuthorize;
import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.QueryResult;
import cn.jianyongjie.org.domain.Employee;
import cn.jianyongjie.org.query.EmployeeQuery;
import cn.jianyongjie.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: TODO
* @Author: Jian Yong Jie
* @Version: 1.0
* @Date:  2022/4/22 9:50
*/
@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;

    /*
    * @Title: EmployeeController
    * @Description: 查询所有员工
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/23 16:58
    * @Parameters: * @param null
    * @Return
    */
    @GetMapping
    public List<Employee> findEmployees(){
        return iEmployeeService.loadAll();
    }


    /*
     * @Title: DepartmentController
     * @Description: 根据部门id查询员工数据
     * @Author: JianYongJie
     * @Version: 1.0
     * @Date:  2022/4/27 18:45
     * @Parameters: * @param null
     * @Return
     */
    @GetMapping("/findempById/{id}")
    public List<Employee> findEmpById(@PathVariable("id") Long id){
        return iEmployeeService.findEmpById(id);
    }

    /*
    * @Title: EmployeeController
    * @Description: 根据id查询数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 9:55
    * @Parameters: * @param id
    * @Return cn.jianyongjie.org.domain.Employee
    */
    // 使用restful风格，设置请求
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findEmployeeById(@ApiParam(name="ID属性",required = true) @PathVariable("id") long id){

        return iEmployeeService.loadById(id);
    }

    /*
    * @Title: EmployeeController
    * @Description: 分页查询
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 10:01
    * @Parameters: * @param
    * @Return cn.jianyongjie.basic.utils.QueryResult
    */
    @PostMapping
    public QueryResult<Employee> findemployees(@RequestBody EmployeeQuery employeeQuery){
        return iEmployeeService.queryData(employeeQuery);
    }

    /*
    * @Title: EmployeeController
    * @Description: 根据id删除数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 12:30
    * @Parameters: * @param
    * @Return void
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据id删除数据",value = "employee:delete")
    public AjaxResult delEmployee(@PathVariable("id") Long id){
        try {
            // 根据id删除数据
            iEmployeeService.remove(id);
            // 返回Ajax
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: EmployeeController
    * @Description: 新增或者修改
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/25 12:25
    * @Parameters: * @param employee
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Employee employee){
        try {
            // 根据id进行新增或者修改
            if (employee.getId()==null){
                // 进行新增
                iEmployeeService.save(employee);
            }else {
                // 进行修改
                iEmployeeService.update(employee);
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: EmployeeController
    * @Description: 批量删除
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/23 12:32
    * @Parameters: * @param ids
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PatchMapping
    public  AjaxResult delEmployees(@RequestBody List<Long> ids){
        try {
            iEmployeeService.batchRemove(ids);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error();
        }
    }

}
